package com.example.helloworld;


import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 自定义区域信息解析器
 * @author 张岩
 * @date 2022/3/23 16:05
 */
public class MyLocalResolver implements LocaleResolver {


	@Override
	public Locale resolveLocale(HttpServletRequest request) {
		//获取请求中参数
		String l = request.getParameter("l");
		//获取默认的区域信息解析器
		Locale locale = Locale.getDefault();
		//根据请求中的参数重新构造区域信息对象
		if (StringUtils.hasText(l)) {
			String[] s = l.split("_");
			locale = new Locale(s[0], s[1]);
		}
		return locale;
	}

	@Override
	public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

	}
}
